#!/bin/sh

htm='<script type="text/javascript" src="/szcg/library/urban/bootstrap.js'
#html=`curl --connect-timeout 5 --max-time 10  http://172.18.13.107:8081/szcg/main.htm | grep bootstrap.js `

echo $htm
echo $html
PNAME='tomcat'

while true

do

sleep 5

html=`curl --connect-timeout 5 --max-time 10  http://172.18.13.102:8081/szcg/main.htm | grep bootstrap.js `

if [[ "$html" =~ "$htm" ]]; 
then
    time=`date +%F--%T`
    echo "当前时间："$time"，szcg web server is good~~ "
    echo "-----------------------------------------------------------------------------"
    sleep 120
else
    echo " I will kill szcg web server process! "  
    #pid=`top -n 1 | grep java | head -n 1 | awk '{print $1}'`
    pid=`ps -ef | grep "$PNAME" | grep -v "$0" | grep -v "grep" | grep -v "tail" | awk '{print $2}'`
    #echo $pid
    
    now_time=`date +%F--%T | sed 's/ /-/g'| sed 's/:/-/g'`
    echo $now_time
    jstack $pid > jstack-${now_time}.txt
    top -n 1 > top-${now_time}.txt
    top -H -n 1 > topH-${now_time}.txt
    jmap -histo $pid > jmap-${now_time}.txt
    #echo $pid
    TOMCAT_ID=`ps -ef | grep "$PNAME" | grep -v "$0" | grep -v "grep" | grep -v "tail" | awk '{print $2}'`
    #echo $TOMCAT_ID
    kill -9 $TOMCAT_ID
    sleep 5
    /szcg/apache-tomcat-7.0.69/bin/startup.sh
    echo '服务器已经重启，等待15分钟重新检测。。。。。'
    sleep 900 
fi

done

exit 0

